<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions direction="vertical" :column="2" border>
            <el-descriptions-item label="申请日期">{{ ruleForm.apply_date }}</el-descriptions-item>
            <el-descriptions-item label="申请付款金额">{{ ruleForm.price }}</el-descriptions-item>
            <el-descriptions-item label="付款原因说明">{{ ruleForm.content }}</el-descriptions-item>
            <el-descriptions-item label="付款方式">{{ ruleForm.payment_type }}</el-descriptions-item>
            <el-descriptions-item label="收款方信息">{{ ruleForm.pay_info }}</el-descriptions-item>
        </el-descriptions>
        <el-form-item label="支付流水号" prop="review_comment">
            <el-input v-model="ruleForm.review_comment" />
        </el-form-item>
        <el-form-item label="支付日期" prop="review_comment">
            <el-input v-model="ruleForm.review_comment" />
        </el-form-item>
        <el-form-item label="支付凭证" prop="review_comment">
            <el-input v-model="ruleForm.review_comment" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" v-if="ruleForm.reviewer_id == 0" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    reviewer_id:number
    status: string
    review_comment:string
    apply_date:string
    price:number
    content:string
    payment_type:string
    pay_info:string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    reviewer_id:0,
    status: '',
    review_comment:'/',
    apply_date:'',
    price:0,
    content:'/',
    payment_type:'/',
    pay_info:'/'
})

const onGetData = () => {
    http.get('/paymentApply/'+ruleForm.value.id).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    status: [
        { required: true, message: '请选择操作类型', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            http.patch('/paymentApply',ruleForm.value).then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
